package net.itheima.mm.controller;

import lombok.extern.slf4j.Slf4j;
import net.itheima.mm.entity.PageResult;
import net.itheima.mm.entity.QueryPageBean;
import net.itheima.mm.entity.Result;
import net.itheima.mm.pojo.Course;
import net.itheima.mm.pojo.Tag;
import net.itheima.mm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhangyan
 * @date 2021/2/27 21:24
 */
@RestController
@RequestMapping("/tag")
@Slf4j
public class TagController {

    @Autowired
    TagService tagService;

    @RequestMapping("/findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = new PageResult();

        log.debug("findListByPage:" + queryPageBean);
        List<Tag> list = tagService.findListByPage(queryPageBean);

        long total = tagService.getTotal();

        pageResult.setTotal(total);
        pageResult.setRows(list);

        return new Result(true,"标签查询成功",pageResult);

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Tag tag) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        tag.setCreateDate(dateString);

        log.debug("add:" + tag);
        tagService.add(tag);

        return new Result(true, "添加成功!");
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Tag tag){
        tagService.update(tag);
        return new Result(true,"修改成功!");
    }

    @RequestMapping("/delete")
    public Result delete(Integer tagId) {
        tagService.delete(tagId);

        return new Result(true, "删除成功!");
    }
}
